﻿using System;
using System.Data;
using System.Text;

namespace ltModel.DAL
{
    public class article_class
    {
        public article_class()
        { }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int class_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from lt_article_class where class_id=" + class_id + "");
            return DataObject.Exists(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ltModel.Model.article_class model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into lt_article_class(");
            strSql.Append("classname,father_id,ceng_num,beizhu,sub_class,Qx,px,model_id");
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append("'" + model.classname + "',");
            strSql.Append("" + model.father_id + ",");
            strSql.Append("'" + model.ceng_num + "',");
            strSql.Append("'" + model.beizhu + "',");
            strSql.Append("" + model.sub_class + ",");
            strSql.Append("" + model.Qx + ",");
            strSql.Append("" + model.px + ",");
            strSql.Append("" + model.model_id + "");
            strSql.Append(")");
            strSql.Append(";select @@IDENTITY");
            object obj = DataObject.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ltModel.Model.article_class model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update lt_article_class set ");
            if (model.classname != null)
                strSql.Append("classname='" + model.classname + "',");
            if (model.father_id.Equals(Int32.Parse("0")) != true)
                strSql.Append("father_id=" + model.father_id + ",");
            if (model.ceng_num != null)
                strSql.Append("ceng_num='" + model.ceng_num + "',");
            if (model.beizhu != null)
                strSql.Append("beizhu='" + model.beizhu + "',");
            if (model.sub_class != null)
                strSql.Append("sub_class=" + model.sub_class + ",");
            if (model.Qx.Equals(Int32.Parse("0")) != true)
                strSql.Append("Qx=" + model.Qx + ",");
            if (model.px.Equals(Int32.Parse("0")) != true)
                strSql.Append("px=" + model.px + ",");
            strSql.Append("model_id=model_id ");
            strSql.Append(" where class_id=" + model.class_id + "");
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int class_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete lt_article_class ");
            strSql.Append(" where class_id=" + class_id);
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ltModel.Model.article_class GetModel(int class_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  ");
            strSql.Append(" [class_id],[classname],[father_id],[ceng_num],[beizhu],[sub_class],[Qx],[px],[model_id] ");
            strSql.Append(" from lt_article_class ");
            strSql.Append(" where class_id=" + class_id);
            ltModel.Model.article_class model = new ltModel.Model.article_class();
            DataSet ds = DataObject.Query(strSql.ToString());
            model.class_id = class_id;
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.classname = ds.Tables[0].Rows[0]["classname"].ToString();
                if (ds.Tables[0].Rows[0]["father_id"].ToString() != "")
                {
                    model.father_id = int.Parse(ds.Tables[0].Rows[0]["father_id"].ToString());
                }
                model.ceng_num = ds.Tables[0].Rows[0]["ceng_num"].ToString();
                model.beizhu = ds.Tables[0].Rows[0]["beizhu"].ToString();
                if (ds.Tables[0].Rows[0]["sub_class"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["sub_class"].ToString() == "1") || (ds.Tables[0].Rows[0]["sub_class"].ToString().ToLower() == "true"))
                    {
                        model.sub_class = "1";
                    }
                    else
                    {
                        model.sub_class = "0";
                    }
                }

                if (ds.Tables[0].Rows[0]["Qx"].ToString() != "")
                {
                    model.Qx = int.Parse(ds.Tables[0].Rows[0]["Qx"].ToString());
                }
                if (ds.Tables[0].Rows[0]["px"].ToString() != "")
                {
                    model.px = int.Parse(ds.Tables[0].Rows[0]["px"].ToString());
                }
                if (ds.Tables[0].Rows[0]["model_id"].ToString() != "")
                {
                    model.model_id = int.Parse(ds.Tables[0].Rows[0]["model_id"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select [class_id],[classname],[father_id],[ceng_num],[beizhu],[sub_class],[Qx],[px],[model_id] ");
            strSql.Append(" FROM lt_article_class ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DataObject.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string topNum, string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (PageValidate.IsNumber(topNum))
                strSql.Append(" top " + topNum + " ");
            strSql.Append(" [class_id],[classname],[father_id],[ceng_num],[beizhu],[sub_class],[Qx],[px],[model_id] ");
            strSql.Append(" FROM lt_article_class ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DataObject.Query(strSql.ToString());
        }

        /// <summary>
        /// 修改类别排序,返回新序号
        /// </summary>
        /// <param name="fid">父ID</param>
        /// <param name="px">原类别序列</param>
        /// <returns></returns>
        public int UpdateClassPx(int fid, int px)
        {
            int npx = 0;
            string sql = "select top 1 class_id,px from lt_article_class where father_id=" + fid + " order by px desc";
            DataTable dt = new DataTable();
            dt = DataObject.Query(sql).Tables[0];
            if (dt.Rows.Count > 0)
            {
                string classid = dt.Rows[0]["class_id"].ToString();
                string cpx = dt.Rows[0]["px"].ToString();
                dt.Clear();
                sql = "select top 1 px from lt_article_class where '|'+ceng_num+'|' like '%|" + classid + "|%' order by px desc";
                dt = DataObject.Query(sql).Tables[0];
                if (dt.Rows.Count > 0)
                    npx = Int32.Parse(dt.Rows[0]["px"].ToString()) + 1;
                else
                    npx = Int32.Parse(cpx) + 1;
            }
            else
            {
                npx = px + 1;
            }

            sql = "UPDATE lt_article_class SET px=px+1 WHERE px>=" + npx;
            DataObject.ExecuteSql(sql);
            return npx;
        }

        /// <summary>
        /// 返回类别最大序号
        /// </summary>
        /// <returns></returns>
        public int MaxPx()
        {
            string sql = "SELECT MAX(px) FROM lt_article_class";
            DataTable maxdt = DataObject.Query(sql).Tables[0];
            if (maxdt.Rows[0][0].ToString() != "")
            {
                return (Int32.Parse(maxdt.Rows[0][0].ToString()) + 1);
            }
            else
            {
                return 1;
            }
        }

        /// <summary>
        /// 查询单类别最大排序
        /// </summary>
        /// <param name="cid">类别ID</param>
        /// <returns></returns>
        public int ClassMaxPx(int cid)
        {
            string sql = "select top 1 px from lt_article_class where father_id=" + cid + " order by px desc";
            if (DataObject.Query(sql).Tables[0].Rows.Count > 0)
                return Int32.Parse(DataObject.Query(sql).Tables[0].Rows[0]["px"].ToString());
            else
            {
                sql = "select px from lt_article_class where class_id=" + cid;
                return Int32.Parse(DataObject.Query(sql).Tables[0].Rows[0]["px"].ToString());
            }
        }

        /// <summary>
        /// 修改类别属性(是否有子类)
        /// </summary>
        /// <param name="val">修的改值</param>
        /// <param name="classid">修改类别ID</param>
        /// <returns></returns>
        public bool UpdateSubClass(int val, int classid)
        {
            string sql = "UPDATE lt_article_class SET sub_class=" + val + " WHERE class_id=" + classid;
            if (DataObject.ExecuteSql(sql)>0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 修改类别排序
        /// </summary>
        /// <param name="px">原类别序列</param>
        /// <returns></returns>
        public bool UpdateClassPx(int px)
        {
            string sql = "";
            sql = "UPDATE lt_article_class SET px=px+1 WHERE px>=" + (px + 1);
            if (DataObject.ExecuteSql(sql) > 0)
                return true;
            else
                return false;
        }
    }
}